#include <stdint.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>

bool between(uint16_t start, uint16_t end, uint16_t aim);
char get_first_letter(uint16_t wchar);
void get_first_letters(const char *szChinese, char piyinBuf[], const uint32_t maxBufLen, uint32_t *piyinBufLen);

int32_t main()
{
    char buf[1024] = {0};
    uint32_t len = 50;
    const char *szChinese = "qaz123获取汉字拼音";
    get_first_letters(szChinese, &buf, 1024, &len);
    printf("--> %s\n", buf);
    return 0;
}

void get_first_letters(const char *szChinese, char piyinBuf[], const uint32_t maxBufLen, uint32_t *piyinBufLen)
{
    uint8_t chr[3];

    *piyinBufLen = 0;
    uint32_t chineseLen = strlen(szChinese);
    for (uint32_t i = 0; i < chineseLen && *piyinBufLen < maxBufLen; ++i)
    {
        printf("piyinBufLen = %d\n", *piyinBufLen);
        uint8_t c = szChinese[i];
        // 排除askii 码
        if (isascii(c))
        {
            piyinBuf[*piyinBufLen++] = c;
        }
        else
        {
            chr[0] = szChinese[i];
            chr[1] = szChinese[++i];
            chr[2] = 0;

            uint16_t wchr = 0;
            wchr = (chr[0] & 0xff) << 8;
            wchr |= (chr[1] & 0xff);
            printf("wchr = %c\n", get_first_letter(wchr));
            piyinBuf[*piyinBufLen++] = toupper(get_first_letter(wchr));
        }
    }
}

bool between(uint16_t start, uint16_t end, uint16_t aim)
{
    if (start <= aim && aim <= end)
    {
        return true;
    }
    return false;
}

char get_first_letter(uint16_t wchar)
{
    if (between(0xB0A1, 0xB0C4, wchar))
        return 'a';
    if (between(0XB0C5, 0XB2C0, wchar))
        return 'b';
    if (between(0xB2C1, 0xB4ED, wchar))
        return 'c';
    if (between(0xB4EE, 0xB6E9, wchar))
        return 'd';
    if (between(0xB6EA, 0xB7A1, wchar))
        return 'e';
    if (between(0xB7A2, 0xB8c0, wchar))
        return 'f';
    if (between(0xB8C1, 0xB9FD, wchar))
        return 'g';
    if (between(0xB9FE, 0xBBF6, wchar))
        return 'h';
    if (between(0xBBF7, 0xBFA5, wchar))
        return 'j';
    if (between(0xBFA6, 0xC0AB, wchar))
        return 'k';
    if (between(0xC0AC, 0xC2E7, wchar))
        return 'l';
    if (between(0xC2E8, 0xC4C2, wchar))
        return 'm';
    if (between(0xC4C3, 0xC5B5, wchar))
        return 'n';
    if (between(0xC5B6, 0xC5BD, wchar))
        return 'o';
    if (between(0xC5BE, 0xC6D9, wchar))
        return 'p';
    if (between(0xC6DA, 0xC8BA, wchar))
        return 'q';
    if (between(0xC8BB, 0xC8F5, wchar))
        return 'r';
    if (between(0xC8F6, 0xCBF0, wchar))
        return 's';
    if (between(0xCBFA, 0xCDD9, wchar))
        return 't';
    if (between(0xCDDA, 0xCEF3, wchar))
        return 'w';
    if (between(0xCEF4, 0xD188, wchar))
        return 'x';
    if (between(0xD1B9, 0xD4D0, wchar))
        return 'y';
    if (between(0xD4D1, 0xD7F9, wchar))
        return 'z';
    return '\0';
}